package com.baosight.sczx.zm.cc.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.cInterface.cc.QualityCondition;
import com.baosight.sczx.zm.cc.domain.ZMCCExamineConditionDetail;

/**
 * 质量标准化
 * 具体工作项打分
 * <p>Title: ServiceZMCC05.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-28 上午11:21:22  严洪浩  Create
 *</pre>
 */
public class ServiceZMCC06 extends ServiceEPBase {
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS41</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-3上午05:31:26 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCC01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28上午02:33:54 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdConditionId", inInfo.get("fdConditionId"), true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCCExamineConditionDetail.query", new ZMCCExamineConditionDetail());
		
		return outInfo;
	}
	
	/**
	 * 修改-扣分
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCC01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28上午02:33:40 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		boolean validate = this.validate(inInfo);
		if(validate) {
			inInfo.setStatus(-1);
			inInfo.setMsg("该分类的扣分总合不能大于该分类的分数！");
			return inInfo;
		}
		else {
			String fdConditionId = inInfo.getBlock("result").getCellStr(0, "fdConditionId");//主表ID
			EiInfo outInfo = super.update(inInfo, "ZMCCExamineConditionDetail.update");
			QualityCondition.updateExamineCondition(fdConditionId);
			return outInfo;
		}
	}

	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCC01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28上午02:33:32 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCCExamineConditionDetail.delete");
		EiInfo outInfo = this.query(inInfo);
	    outInfo.setMsg(_temp.getMsg());
	    outInfo.setDetailMsg(_temp.getDetailMsg());

		return outInfo;
	}
	
	/**
	 * 判断扣分是否超过
	 * 最大分数
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-22下午01:57:04 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo) {
		String fdKeyId = inInfo.getBlock("result").getCellStr(0, "fdKeyId");
		String fdClassifyId = inInfo.getBlock("result").getCellStr(0, "fdClassifyId");
		Map<String, String> _args = new HashMap<String, String>();
		_args.put("fdKeyId", fdKeyId);
		List<?> _result = dao.query("ZMCCExamineConditionDetail.querySum", _args);
		
		float _cutAll = 0.0f;
		if(_result.size() > 0) {
			Map _map = (Map)_result.get(0);
			if(null == _map || null == _map.get("fdReducePoint") || "".equals(_map.get("fdReducePoint").toString())) {
				
			}
			else {
				_cutAll = Float.parseFloat(_map.get("fdReducePoint").toString());
			}
		}
		
		
		_args.put("fdClassifyId", fdClassifyId);
		_result = dao.query("ZMCCQualitystandardClassify.queryInterface", _args);
		float _point = 0.0f;
		if(_result.size() > 0)  {
			Map _map = (Map)_result.get(0);
			if(null != _map) {
				String fdClassifyPoint = _map.get("fdClassifyPoint") == null ? "0" : _map.get("fdClassifyPoint").toString();
				
				_point = Float.parseFloat(fdClassifyPoint);
			}
		}
		
		if(_cutAll > _point) {
			return true;
		}
		else {
			return false;
		}
	}
}
